#coding=utf-8
#Boa:Dialog:DlgUpdate

import wx

from product import CDataSaleOrder,CRelFundCustomer,CDataVersion
from config import Cfg

def create(parent):
    return DlgUpdate(parent)

[wxID_DLGUPDATE, wxID_DLGUPDATEBTNUPDATE, wxID_DLGUPDATETCINFO, 
 wxID_DLGUPDATETVERSION, 
] = [wx.NewId() for _init_ctrls in range(4)]

class DlgUpdate(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGUPDATE, name=u'DlgUpdate',
              parent=prnt, pos=wx.Point(454, 309), size=wx.Size(454, 435),
              style=wx.DEFAULT_DIALOG_STYLE, title=u'\u8f6f\u4ef6\u5347\u7ea7')
        self.SetClientSize(wx.Size(446, 401))

        self.btnUpdate = wx.Button(id=wxID_DLGUPDATEBTNUPDATE,
              label=u'\u5f00\u59cb\u5347\u7ea7', name=u'btnUpdate', parent=self,
              pos=wx.Point(56, 96), size=wx.Size(328, 24), style=0)
        self.btnUpdate.Bind(wx.EVT_BUTTON, self.OnBtnUpdateButton,
              id=wxID_DLGUPDATEBTNUPDATE)

        self.tcInfo = wx.TextCtrl(id=wxID_DLGUPDATETCINFO, name=u'tcInfo',
              parent=self, pos=wx.Point(56, 144), size=wx.Size(328, 184),
              style=wx.TE_MULTILINE, value=u'')

        self.tVersion = wx.StaticText(id=wxID_DLGUPDATETVERSION,
              label=u'\u8f6f\u4ef6\u7248\u672c\u4e0e\u6570\u636e\u5e93\u7248\u672c\u4e0d\u5339\u914d',
              name=u'tVersion', parent=self, pos=wx.Point(56, 32),
              size=wx.Size(156, 14), style=0)

    def __init__(self, parent):
        self._init_ctrls(parent)
        
        softVer = Cfg.version
        dbVer = CDataVersion.getVersion()
        
        info = u'数据库版本号：%s\n软件  版本号：%s' %(softVer, dbVer)
        self.tVersion.SetLabel(info)
        
    def getFuncList(self, oldVer, newVer):
        self.list = [self.update0_0To0_1,
                    self.update0_1To0_2]

    def update0_0To0_1(self):
        # 重设sale order的paid
        for obj in CDataSaleOrder.m_dataList:
            total = obj.getValue(obj.COL_TOTAL)
            disocunt = obj.getValue(obj.COL_DISCOUNT)
            
            paid = CRelFundCustomer.getPaidAmount(obj.getValue(obj.COL_ID))
            unpaid = total - paid - discount
            
            data = {obj.COL_PAID:paid,
                    obj.COL_UNPAID:unpaid}
            obj.setMulFieldVal(data)
            obj.update()
            
        sql = ''

    def update0_1To0_2(self):
        pass
        
    def OnBtnUpdateButton(self, event):
        self.EndModal(wx.ID_OK)
            
        
